package com.jungle.app.jw.service.impl;

import com.jungle.app.domain.entity.JwSpotFacility;
import com.jungle.app.jw.domain.bo.JwSpotFacilityBo;
import com.jungle.app.jw.domain.vo.JwSpotFacilityVo;
import com.jungle.app.jw.repository.JwSpotFacilityRepository;
import com.jungle.app.jw.service.IJwSpotFacilityService;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 景点设施Service业务层处理
 *
 * @author jungle
 * @date 2023-12-04
 */
@RequiredArgsConstructor
@Service
public class JwSpotFacilityServiceImpl implements IJwSpotFacilityService {

    private final JwSpotFacilityRepository repository;

    /**
     * 查询景点设施
     */
    @Override
    public JwSpotFacilityVo queryById(String id){
        return repository.findById(id).map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwSpotFacilityVo.class)).orElse(null);
    }

    /**
     * 查询景点设施列表
     */
    @Override
    public TableDataInfo<JwSpotFacilityVo> queryPageList(JwSpotFacilityBo bo, PageQuery pageQuery) {
        Page<JwSpotFacility> result = repository.findAll(getSpecification(bo), pageQuery.pageable());
        return TableDataInfo.build(result, JwSpotFacilityVo.class);
    }

    /**
     * 查询景点设施列表
     */
    @Override
    public List<JwSpotFacilityVo> queryList(JwSpotFacilityBo bo) {
        return repository.findAll(getSpecification(bo)).stream()
            .map(v -> com.jungle.common.utils.BeanCopyUtils.copy(v, JwSpotFacilityVo.class))
            .collect(Collectors.toList());
    }

    private Specification<JwSpotFacility> getSpecification(JwSpotFacilityBo bo) {
        return (root, query, cb) -> {
            Map<String, Object> params = bo.getParams();
            List<Predicate> predicates = new ArrayList<>();
            if (bo.getCreateBy() != null) {
                    predicates.add(cb.equal(root.get("createBy"), bo.getCreateBy()));
            }
            if (bo.getCreateTime() != null) {
                    predicates.add(cb.equal(root.get("createTime"), bo.getCreateTime()));
            }
            if (bo.getUpdateBy() != null) {
                    predicates.add(cb.equal(root.get("updateBy"), bo.getUpdateBy()));
            }
            if (bo.getUpdateTime() != null) {
                    predicates.add(cb.equal(root.get("updateTime"), bo.getUpdateTime()));
            }
            if (StringUtils.isNotBlank(bo.getDetails())) {
                    predicates.add(cb.equal(root.get("Details"), bo.getDetails()));
            }
            if (StringUtils.isNotBlank(bo.getFacilityID())) {
                    predicates.add(cb.equal(root.get("FacilityID"), bo.getFacilityID()));
            }
            if (StringUtils.isNotBlank(bo.getIntroduction())) {
                    predicates.add(cb.equal(root.get("Introduction"), bo.getIntroduction()));
            }
            if (StringUtils.isNotBlank(bo.getLocation())) {
                    predicates.add(cb.equal(root.get("Location"), bo.getLocation()));
            }
            if (StringUtils.isNotBlank(bo.getName())) {
                    predicates.add(cb.like(root.get("Name"), "%" + bo.getName() + "%"));
            }
            if (StringUtils.isNotBlank(bo.getOpenTime())) {
                    predicates.add(cb.equal(root.get("OpenTime"), bo.getOpenTime()));
            }
            if (StringUtils.isNotBlank(bo.getScenicSpotID())) {
                    predicates.add(cb.equal(root.get("ScenicSpotID"), bo.getScenicSpotID()));
            }
            if (StringUtils.isNotBlank(bo.getStatus())) {
                    predicates.add(cb.equal(root.get("Status"), bo.getStatus()));
            }
            if (StringUtils.isNotBlank(bo.getType())) {
                    predicates.add(cb.equal(root.get("Type"), bo.getType()));
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        };
    }

    /**
     * 新增景点设施
     */
    @Override
    public Boolean insertByBo(JwSpotFacilityBo bo) {
        JwSpotFacility add = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwSpotFacility.class);
        validEntityBeforeSave(add);
        repository.saveAndFlush(add);
        bo.setId(add.getId());
        return true;
    }

    /**
     * 修改景点设施
     */
    @Override
    public Boolean updateByBo(JwSpotFacilityBo bo) {
        JwSpotFacility update = com.jungle.common.utils.BeanCopyUtils.copy(bo, JwSpotFacility.class);
        validEntityBeforeSave(update);
        return repository.save(update) != null;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwSpotFacility entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除景点设施
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        repository.deleteAllById(ids);
        return true;
    }

}
